package csel.view.status;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import csel.model.*;
import csel.model.entity.Character;

public class StatsMenu extends JPanel implements Observer<Object>
{

	private static final long serialVersionUID = 1L;
	
	private Character myCharacter;
	private CharacterStatsHolder myStats;
	private final MyStatObserver myStatsObserver;
	
	private JLabel lives;
	private JLabel strength;
	private JLabel agility;
	private JLabel intellect;
	private JLabel hardiness;
	private JLabel experience;
	private JLabel movement;
	private JLabel level;
	
	//private JLabel life;
	//private JLabel lifeMax;
	//private JLabel mana;
	//private JLabel manaMax;
	private JLabel offensiveRating;
	private JLabel defensiveRating;
	private JLabel armorRating;
	private JLabel flexBucks;
	private JLabel skillPoints;
	
	private JLabel livesV;
	private JLabel strengthV;
	private JLabel agilityV;
	private JLabel intellectV;
	private JLabel hardinessV;
	private JLabel experienceV;
	private JLabel movementV;
	private JLabel levelV;
	//private JLabel lifeV;
	
	//private JLabel lifeMaxV;
	//private JLabel manaV;
	//private JLabel manaMaxV;
	private JLabel offensiveRatingV;
	private JLabel defensiveRatingV;
	private JLabel armorRatingV;
	private JLabel flexBucksV;
	private JLabel skillPointsV;
	
	public static StatsMenu initialize(Character myCharacter)
	{
		StatsMenu theStatsMenu = new StatsMenu(myCharacter);
		myCharacter.addObserver(theStatsMenu);
		theStatsMenu.myStats.addObserver(theStatsMenu.myStatsObserver);
		return theStatsMenu;
	}
	
	private StatsMenu(Character myCharacter)
	{
		this.myCharacter = myCharacter;
		myStats = myCharacter.getCharacterStatsHolder();
		
		myStatsObserver = new MyStatObserver();
		
		lives = makeLabel("Lives: ");
		strength = makeLabel("Strength: ");
		agility = makeLabel("Agility: ");
		intellect = makeLabel("Intellect: ");
		hardiness = makeLabel("Hardiness: ");
		experience = makeLabel("Experience: ");
		movement = makeLabel("Movement: ");
		level = makeLabel("Level: ");
		//life = makeLabel("Life: ");
		//lifeMax = makeLabel("Max Life: ");
		//mana = makeLabel("Mana: ");
		//manaMax = makeLabel("Max Mana: ");
		offensiveRating = makeLabel("Offense: ");
		defensiveRating = makeLabel("Defense: ");
		armorRating = makeLabel("Armor: ");
		flexBucks = makeLabel("Flexbucks: ");
		skillPoints = makeLabel("Skill Points: ");
		
		livesV = makeNumLabel(myStats.getStat(CharacterStats.Lives));
		strengthV = makeNumLabel(myStats.getStat(CharacterStats.Strength));
		agilityV = makeNumLabel(myStats.getStat(CharacterStats.Agility));
		intellectV = makeNumLabel(myStats.getStat(CharacterStats.Intellect));
		hardinessV = makeNumLabel(myStats.getStat(CharacterStats.Hardiness));
		experienceV = makeNumLabel(myStats.getStat(CharacterStats.Experience));
		movementV = makeNumLabel(myStats.getStat(CharacterStats.Speed));
		levelV = makeNumLabel(myStats.getStat(CharacterStats.Level));
		//lifeV = makeNumLabel(myStats.getStat(CharacterStats.CurrentLife));
//		lifeMaxV = makeNumLabel(myStats.getStat(CharacterStats.MaxLife));
//		manaV = makeNumLabel(myStats.getStat(CharacterStats.CurrentMana));
//		manaMaxV = makeNumLabel(myStats.getStat(CharacterStats.MaxMana));
		offensiveRatingV = makeNumLabel(myStats.getStat(CharacterStats.Offense));
		defensiveRatingV = makeNumLabel(myStats.getStat(CharacterStats.Defense));
		armorRatingV = makeNumLabel(myStats.getStat(CharacterStats.Armor));
		flexBucksV = makeNumLabel(myCharacter.getFlexBucks());
		skillPointsV = makeNumLabel(myCharacter.getFreeSkillPoints());
		
		
		//statsPanel = new JPanel();
		
		JPanel labelsPanel1 = new JPanel();
		labelsPanel1.setOpaque(false);
		labelsPanel1.setLayout(new BoxLayout(labelsPanel1, BoxLayout.PAGE_AXIS));
		
		JPanel valuesPanel1 = new JPanel();
		valuesPanel1.setOpaque(false);
		valuesPanel1.setLayout(new BoxLayout(valuesPanel1, BoxLayout.PAGE_AXIS));
		
		JPanel labelsPanel2 = new JPanel();
		labelsPanel2.setOpaque(false);
		labelsPanel2.setLayout(new BoxLayout(labelsPanel2, BoxLayout.PAGE_AXIS));
		
		JPanel valuesPanel2 = new JPanel();
		valuesPanel2.setOpaque(false);
		valuesPanel2.setLayout(new BoxLayout(valuesPanel2, BoxLayout.PAGE_AXIS));
		
		labelsPanel1.add(lives);
		labelsPanel1.add(strength);
		labelsPanel1.add(agility);
		labelsPanel1.add(intellect);
		labelsPanel1.add(hardiness);
		labelsPanel1.add(experience);
		labelsPanel1.add(movement);
		labelsPanel2.add(level);
//		labelsPanel.add(life);
//		labelsPanel.add(lifeMax);
//		labelsPanel.add(mana);
//		labelsPanel.add(manaMax);
		labelsPanel2.add(offensiveRating);
		labelsPanel2.add(defensiveRating);
		labelsPanel2.add(armorRating);
		labelsPanel2.add(flexBucks);
		labelsPanel2.add(skillPoints);
		

		valuesPanel1.add(livesV);
		valuesPanel1.add(strengthV);
		valuesPanel1.add(agilityV);
		valuesPanel1.add(intellectV);
		valuesPanel1.add(hardinessV);
		valuesPanel1.add(experienceV);
		valuesPanel1.add(movementV);
		valuesPanel2.add(levelV);
//		valuesPanel.add(lifeV);
//		valuesPanel.add(lifeMaxV);
//		valuesPanel.add(manaV);
//		valuesPanel.add(manaMaxV);
		valuesPanel2.add(offensiveRatingV);
		valuesPanel2.add(defensiveRatingV);
		valuesPanel2.add(armorRatingV);
		valuesPanel2.add(flexBucksV);
		valuesPanel2.add(skillPointsV);
		
		JPanel leftPanel = new JPanel();
		leftPanel.setOpaque(false);
		JPanel rightPanel = new JPanel();
		rightPanel.setOpaque(false);
		
		leftPanel.add(labelsPanel1,BorderLayout.WEST);
		leftPanel.add(valuesPanel1,BorderLayout.EAST);
		rightPanel.add(labelsPanel2,BorderLayout.WEST);
		rightPanel.add(valuesPanel2,BorderLayout.EAST);
		
		add(leftPanel,BorderLayout.WEST);
		add(rightPanel,BorderLayout.EAST);
		
		//myScrollPane = new JScrollPane(statsPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		/*sta.setPreferredSize(new Dimension(375,125));
		add(myScrollPane);*/
		setPreferredSize(new Dimension(375,125));
		setOpaque(false);
		
	}
	
	public void mupdate(Object o, Object param)
	{
		if (o.equals(myCharacter))
		{
			resetCharInfo();
		}
	}
	
	private class MyStatObserver implements Observer<CharacterStats>
	{
		public void mupdate(Object o, CharacterStats cs)
		{
			switch (cs)
			{
				case Lives:
					resetStatLabel(livesV,cs);
				break;
				case Strength:
					resetStatLabel(strengthV,cs);
				break;
				case Agility:
					resetStatLabel(agilityV,cs);
				break;
				case Intellect:
					resetStatLabel(intellectV,cs);
				break;
				case Hardiness:
					resetStatLabel(hardinessV,cs);
				break;
				case Experience:
					resetStatLabel(experienceV,cs);
				break;
				case Speed:
					resetStatLabel(movementV,cs);
				break;
				case Level:
					resetStatLabel(levelV,cs);
				break;
//				case CurrentLife:
//					resetStatLabel(lifeV,cs);
//				break;
//				case MaxLife:
//					resetStatLabel(lifeMaxV,cs);
//				break;
//				case CurrentMana:
//					resetStatLabel(manaV,cs);
//				break;
//				case MaxMana:
//					resetStatLabel(manaMaxV,cs);
//				break;
				case Offense:
					resetStatLabel(offensiveRatingV,cs);
				break;
				case Defense:
					resetStatLabel(defensiveRatingV,cs);
				break;
				case Armor:
					resetStatLabel(armorRatingV,cs);
				break;
			}
		}
	}
	
	private JLabel makeLabel(String s)
	{
		JLabel tempLabel = new JLabel(s);
		tempLabel.setForeground(Color.white);
		return(tempLabel);
	}
	
	private JLabel makeNumLabel(Integer i)
	{
		JLabel tempLabel = new JLabel(i.toString());
		tempLabel.setForeground(Color.white);
		return(tempLabel);
	}
	
	private void resetStatLabel(JLabel myLabel, CharacterStats cs)
	{
		myLabel.setText(Integer.toString(myStats.getStat(cs)));
		myLabel.revalidate();
		myLabel.repaint();
	}
	
	private void resetCharInfo()
	{
		flexBucksV.setText(Integer.toString(myCharacter.getFlexBucks()));
		skillPointsV.setText(Integer.toString(myCharacter.getFreeSkillPoints()));
	}
	
	public void setCharacter(Character myCharacter)
	{
		if (this.myCharacter != null)
		{
			this.myCharacter.removeObserver(this);
			// FIXME remove cs observer
		}
		
		this.myCharacter = myCharacter;
		myStats = myCharacter.getCharacterStatsHolder();
		myStats.addObserver(myStatsObserver);
		myCharacter.addObserver(this);
	}

	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("StatsMenu should not be serialized.");
    }
    
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("StatsMenu should not be serialized.");
    }

	
}
